/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.far.aus.adm;

import com.far.aus.adm.clas.FormaPago;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Jorge
 */
public class FrmFormaPagoGuardar extends HttpServlet {

    private String _ip = null;
    private int _puerto = 1433;
    private String _db = null;
    private String _usuario = null;
    private String _clave = null;
    private int _fxp = 50;

    public void init(ServletConfig config) throws ServletException
    {
        this._ip = config.getServletContext().getInitParameter("_IP");
        this._puerto = Integer.parseInt(config.getServletContext().getInitParameter("_PUERTO"));
        this._db = config.getServletContext().getInitParameter("_DB");
        this._usuario = config.getServletContext().getInitParameter("_USUARIO");
        this._clave = config.getServletContext().getInitParameter("_CLAVE");
        this._fxp = Integer.parseInt(config.getServletContext().getInitParameter("_PAGINACION"));
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "Mon, 01 Jan 2001 00:00:01 GMT");
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Cache-Control", "must-revalidate");
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        
        String WHERE = request.getParameter("WHERE");
        String p = request.getParameter("p") != null ? request.getParameter("p") : "0";

        String id = request.getParameter("id");
        String forma_pago = request.getParameter("forma_pago");
        String estado = request.getParameter("estado");
        String res = "msg»Nombre de forma de pago ya existe";
        
        FormaPago objFormaPago = new FormaPago(this._ip, this._puerto, this._db, this._usuario, this._clave);
        if(!objFormaPago.nombreExiste(id, forma_pago)){
            if(id.compareTo("-1")==0){
                if(objFormaPago.insertar(forma_pago, estado)){
                    res = "tbl»"+objFormaPago.paginarJSON("vta_tipo_forma_pago", "id_tipo_forma_pago,forma_pago,txt_estado", WHERE, Integer.parseInt(p), this._fxp)+
                            "^id»-1^msg»Información ingresada satisfactoriamente.";
                }else{
                    res = "msg»" + objFormaPago.getError();
                }
            }else{
                if(objFormaPago.actualizar(id, forma_pago, estado)){
                    res = "tbl»"+objFormaPago.paginarJSON("vta_tipo_forma_pago", "id_tipo_forma_pago,forma_pago,txt_estado", WHERE, Integer.parseInt(p), this._fxp)+
                                "^msg»Información guardada satisfactoriamente.";
                }else{
                        res = "msg»" + objFormaPago.getError();
                    }
            }
        }
        
        try{
            out.print(res);
        }finally {
            objFormaPago.cerrar();
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
